package com.welink.cmp.meatdata.command;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.welink.cmp.convertor.GenericConvertor;
import com.welink.cmp.dao.metadata.HostDaoService;
import com.welink.cmp.dao.metadata.dataobject.HostDO;
import com.welink.cmp.domain.metadata.gateway.IHostGateway;
import com.welink.cmp.dto.metadata.MetadataIdListQry;
import com.welink.cmp.dto.metadata.host.CreateHostAddCmd;
import com.welink.cmp.dto.metadata.host.HostUpdCmd;
import com.welink.cmp.dto.metadata.host.clientobject.HostBasicCO;
import com.welink.cmp.dto.metadata.host.clientobject.HostCO;
import com.welink.cmp.dto.metadata.host.query.HostListByPageQry;
import com.welink.common.dto.PageResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Host命令执行类
 * 负责处理Host相关的业务逻辑和数据操作
 */
@Component
public class HostCmdExe {

    @Autowired
    private HostDaoService hostDaoService;

    @Autowired
    private IHostGateway hostGateway;

    /**
     * 保存Host信息
     *
     * @param addCmd 添加Host的命令对象
     * @return 新增Host的ID
     */
    public Long save(CreateHostAddCmd addCmd) {
        HostDO hostDO = new HostDO();
        BeanUtils.copyProperties(addCmd, hostDO);
        if (addCmd.isNeedsOperator()){
            hostDO.setCreateBy(addCmd.getOperator());
        }
        hostDO = hostDaoService.saveHost(hostDO);
        return hostDO.getId();
    }

    /**
     * 更新Host信息
     *
     * @param updCmd 更新Host的命令对象
     * @return 更新是否成功
     */
    public Boolean update(HostUpdCmd updCmd) {
        HostDO hostDO = new HostDO();
        BeanUtils.copyProperties(updCmd, hostDO);
        if (updCmd.isNeedsOperator()){
            hostDO.setUpdateBy(updCmd.getOperator());
        }
        return hostDaoService.updateById(hostDO);
    }

    public HostDO queryByAssetsId(String assetsId){
        LambdaQueryWrapper<HostDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HostDO::getAssetsId,assetsId);
        return hostDaoService.getOne(wrapper);
    }

    /**
     * 根据ID查询Host信息
     *
     * @param id Host ID
     * @return Host业务对象
     */
    public HostCO queryById(Long id) {
        HostDO hostDO = hostDaoService.getById(id);
        HostCO hostCO = new HostCO();
        BeanUtils.copyProperties(hostDO, hostCO);
        return hostCO;
    }

    /**
     * 列表查询Host信息
     *
     * @param listQry 列表查询条件
     * @return Host业务对象列表
     */
    public List<HostCO> listQuery(MetadataIdListQry listQry) {
        LambdaQueryWrapper<HostDO> wrapper = new LambdaQueryWrapper<>();
        // 根据列表查询条件设置查询条件
        wrapper.in(HostDO::getId,listQry.getIdList());
        List<HostDO> hostDOList = hostDaoService.list(wrapper);
        if (CollectionUtils.isEmpty(hostDOList)){
            return new ArrayList<>();
        }
        return hostDOList.stream().map(hostDO -> {
            HostCO hostCO = new HostCO();
            BeanUtils.copyProperties(hostDO, hostCO);
            return hostCO;
        }).collect(Collectors.toList());
    }

    /**
     * 分页查询Host信息
     *
     * @param pageQry 分页查询条件
     * @return 分页响应，包括Host列表和分页信息
     */
    public PageResponse<HostBasicCO> pageQuery(HostListByPageQry pageQry) {
        final Page<HostDO> pageR = hostDaoService.pageQry(pageQry);
        final List<HostDO> records = pageR.getRecords();
        final List<HostBasicCO> coList = GenericConvertor.toCOList(records,HostBasicCO.class);
        return PageResponse.of(coList,pageR.getTotal(),pageR.getSize(),pageR.getCurrent());
    }


}